package com.xl.studentpass.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.xl.studentpass.entity.UserEntity;
import com.xl.studentpass.service.LoginService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: xl
 * @createTime: 2023/5/24 10:12:02
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description: 登录接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "登录相关接口")
public class LoginController {

    @Resource
    RedisTemplate<String, String> redisTemplate;
    @Resource
    LoginService loginService;
    @Value("${password.fKey}")
    private String key;

    @PostMapping("/loginAndRegister")
    @ApiOperation("注册或者登录")
    public R longinAndRegister(@RequestBody Map<String, String> userVo) {
        log.info(this.getClass().getSimpleName() + "==========================注册或者登录");
        if (ObjectUtils.isEmpty(userVo) || ObjectUtils.isEmpty(userVo.get("phone")) || ObjectUtils.isEmpty(userVo.get("code"))) {
            return R.error("输入内容错误");
        }
        return loginService.loginAndRegister(userVo);
    }

    @PostMapping("/login")
    @ApiOperation("登录接口")
    public R longin(@RequestBody Map<String, String> userVo) {
        log.info(this.getClass().getSimpleName() + "====================================登录接口");
        if (ObjectUtils.isEmpty(userVo) || ObjectUtils.isEmpty(userVo.get("phone")) || ObjectUtils.isEmpty(userVo.get("password"))) {
            return R.error("输入内容错误");
        }
        return loginService.login(userVo);
    }

    @PostMapping("/sendMessage")
    @ApiOperation("发送验证码")
    public R sendMessage(@RequestBody Map<String, String> phoneMap, HttpServletRequest request) {
        log.info(this.getClass().getSimpleName() + "=====================================发送验证码");
        return loginService.sendMessage(phoneMap, request);
        //return loginService.sendMessage(phoneMap.get("phone"));
    }

    @GetMapping("/checkUserExist")
    @ApiOperation("检查用户是否存在")
    public R checkUserExist(@RequestParam String phone) {
        log.info(this.getClass().getSimpleName() + "================================检查用户是否存在");
        UserEntity flag = loginService.checkUserExistByPhone(phone);
        return R.ok().put("check", !ObjectUtils.isEmpty(flag));
    }


    @PostMapping("/logout")
    @ApiOperation("登出")
    public R logout() {
        log.info(this.getClass().getSimpleName() + "=========================================登出");
        StpUtil.logout();
        return R.ok();
    }

    @GetMapping("/getKey")
    @ApiOperation("获取前端密码加密key")
    public R getKey() {
        log.info(this.getClass().getSimpleName() + "==========================================获取前端密码加密key");
        return R.ok().put("key", key);
    }
}